package com.ygqh.baby.controller.admin;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.httpclient.HttpException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.foxinmy.weixin4j.http.HttpClientException;
import com.foxinmy.weixin4j.payment.mch.RefundRecord;
import com.foxinmy.weixin4j.util.StringUtil;
import com.ygqh.baby.ao.CodeType;
import com.ygqh.baby.ao.Message;
import com.ygqh.baby.ao.OrderStatus;
import com.ygqh.baby.ao.PayType;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.ao.ReturnStatus;
import com.ygqh.baby.ao.ReturnType;
import com.ygqh.baby.controller.utils.DocumentHandler;
import com.ygqh.baby.exception.YgException;
import com.ygqh.baby.model.YgReturnOrderInfoModel;
import com.ygqh.baby.model.wdt.WdtResult;
import com.ygqh.baby.model.wdt.qyb.WdtQybResult;
import com.ygqh.baby.po.AdminUser;
import com.ygqh.baby.po.YgAlipayRefundRecord;
import com.ygqh.baby.po.YgOrder;
import com.ygqh.baby.po.YgReturnOrder;
import com.ygqh.baby.po.YgReturnOrderDetail;
import com.ygqh.baby.po.YgReturnOrderLog;
import com.ygqh.baby.service.FileService;
import com.ygqh.baby.service.YgAlipayRefundRecordService;
import com.ygqh.baby.service.YgFrameCodeService;
import com.ygqh.baby.service.YgOrderService;
import com.ygqh.baby.service.YgReturnLogService;
import com.ygqh.baby.service.YgReturnOrderDetailService;
import com.ygqh.baby.service.YgReturnOrderLogService;
import com.ygqh.baby.service.YgReturnOrderService;
import com.ygqh.baby.service.alipay.util.AlipayNotify;
import com.ygqh.baby.service.alipay.util.AlipaySubmit;
import com.ygqh.baby.service.task.WdtService;
import com.ygqh.baby.service.wdtqyb.api.WdtQybApi;
import com.ygqh.baby.service.wdtqyb.exception.WdtException;
import com.ygqh.baby.service.wechat.YgWeChatService;
import com.ygqh.baby.shiro.utils.SessionUtil;
import com.ygqh.baby.utils.DateConvertUtils;
import com.ygqh.baby.utils.ThreadPool;

@Controller
@RequestMapping("/agent/returnorder")
public class ReturnOrderController extends BaseController<Object, java.lang.Long> {

	@Autowired
	private YgReturnOrderService ygReturnOrderService;
	@Autowired
	private YgReturnOrderDetailService ygReturnOrderDetailService;
	@Autowired
	private YgReturnLogService ygReturnLogService;
	@Autowired
	private YgReturnOrderLogService ygReturnOrderLogService;
	@Autowired
	private FileService fileService;
	@Autowired
	private YgOrderService ygOrderService;
	@Autowired
	private YgFrameCodeService ygFrameCodeService;
	
	@Autowired
	private ThreadPool threadPool;	

	@RequestMapping("searchOrder")
	@ResponseBody
	public Map<String, Object> searchOrder(String q, QueryInfo queryInfo, Date startDate, Date endDate,
			ReturnStatus status, Long orderId, String returnCode, ReturnType returnType,
			String expressCode,String productName) {
		ResultSet<YgReturnOrder> resultSet = ygReturnOrderService.searchReturnForAgent(queryInfo, getSupplier().getId(), returnType, returnCode, startDate, endDate, orderId, status, expressCode,productName);
		return resultSet.toDatatables(null);
	}

	@RequestMapping("returnorderDetail")
	@ResponseBody
	public YgReturnOrderInfoModel returnorderDetail(String returnCode, Long userId) {

		return ygReturnOrderService.findReturnOrderInfo(returnCode, userId);
	}

	@RequestMapping("updateQuestionDesc")
	@ResponseBody
	public Message updateQuestionDesc(String desc, String returnCode, Long rodId) {
	
		YgReturnOrderDetail ygReturnOrderDetail = new YgReturnOrderDetail(rodId, desc);
		ygReturnOrderDetailService.update(ygReturnOrderDetail);
		ygReturnLogService.addReturnLog(returnCode, rodId, "更新明细处理方式",getSupplier().getSupplierName(), "rodId=" + rodId + ";"
				+ desc);
		return Message.success(null);
	}
	@RequestMapping("userSendOrder")
	@ResponseBody
	public Message userSendOrder(String returnCode, Long id, String userExpressCompany, String userExpressCode) {
		YgReturnOrder ygReturnOrder = ygReturnOrderService.findById(id);
		
		YgOrder order = ygOrderService.findByOrderId(ygReturnOrder.getOrderId());
		ygReturnOrder.setId(id);
		ygReturnOrder.setUserExpressCode(userExpressCode);
		ygReturnOrder.setUserExpressCompany(userExpressCompany);
		ygReturnOrder.setUpdateBy(getSupplier().getSupplierName());
		ygReturnOrder.setUpdateTime(new Date());
		ygReturnOrder.setReturnStatus(ReturnStatus.WaitingInStock);
		ygReturnOrder.setReceiver(order.getReceiver());
		ygReturnOrder.setProvinceName(order.getProvinceName());
		ygReturnOrder.setCityName(order.getCityName());
		ygReturnOrder.setAreaName(order.getAreaName());
		ygReturnOrder.setAddress(order.getAddress());
		ygReturnOrder.setTelPhone(order.getTelPhone());
		ygReturnOrderService.update(ygReturnOrder);
		ygReturnOrderLogService.addReturnOrderLog(returnCode,
				"您的退货已寄回，等待入库。物流信息：" + ygReturnOrder.getUserExpressCompany() + ygReturnOrder.getUserExpressCode(),
				ReturnStatus.WaitingInStock, getSupplier().getSupplierName(), null);
		return SUCCESS_MESSAGE;

	}

	/**
	 * 退换货入库标记
	 * 
	 * @param returnorderCode
	 * @param remark
	 * @return
	 */
	@RequestMapping("returnOrderInstock")
	@ResponseBody
	public Message returnOrderInstock(String skuList, String remark) {
		List<YgReturnOrderDetail> stockInModels = JSON.parseObject(skuList,
				new TypeReference<List<YgReturnOrderDetail>>() {
				});
		return ygReturnOrderService.saveStockInOrder(stockInModels, getSupplier().getSupplierName());
	}

	@RequestMapping("getReturnLogs")
	@ResponseBody
	public List<YgReturnOrderLog> getReturnLogs(String returnCode) {
		return ygReturnOrderLogService.getReturnScheduleList(returnCode);

	}

	@RequestMapping("sendOrder")
	@ResponseBody
	public Message sendOrder(String returnCode, Long id, String expressCompany, String expressCode,
			String logisticsCode, int goodAmount) {
		return ygReturnOrderService.sendOutGoods(returnCode, expressCompany, logisticsCode, expressCode,
				Long.valueOf(goodAmount), getSupplier().getSupplierName());

	}

	@RequestMapping("exportOrder")
	public void exportOrder(Date startDate, Date endDate, String returnType, HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		endDate = DateConvertUtils.addDay(endDate, 1);

		List<Map<String, Object>> list = ygReturnOrderService.getReturnOrderReport(startDate, endDate,
				returnType.equals("-1") ? null : returnType,null);

		byte[] bytes = fileService.downloadFromOss("template/returnorderlist.xls"); 
		List<Map<String, Object>> dataMaps = new ArrayList<Map<String, Object>>();
		Map<String, Object> dataMap = new HashMap<String, Object>();

		dataMap.put("resultSet", list);
		dataMaps.add(dataMap);
		DocumentHandler.DownloadExcel(bytes, "退换货订单统计.xls", dataMap, request, response);
	}
}
